package me.maiz.babyisland.admin;

import me.maiz.babyisland.admin.asynctask.event.ElasticSearchIndexEvent;
import me.maiz.babyisland.dao.ProductMapper;
import me.maiz.babyisland.dao.model.Product;
import com.google.common.eventbus.AsyncEventBus;
import lombok.extern.slf4j.Slf4j;
import me.maiz.babyisland.admin.asynctask.event.ElasticSearchIndexEvent;
import me.maiz.babyisland.dao.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/admin/product")
@Slf4j
public class AdminProductController {

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private AsyncEventBus asyncEventBus;

    @RequestMapping("add")
    public String toAdd(){
        return "admin/product/add";
    }

    @RequestMapping("doAdd")
    public String doAdd(Product p){
        //将数据同步插入到数据库
        int insert = productMapper.insert(p);
        log.info("{}",p);
        //将数据异步索引到搜索引擎
        asyncEventBus.post(new ElasticSearchIndexEvent<Product>(p));

        return "redirect:add";
    }


}
